<!-- 模板区域 -->
<template>
  <div class="login_container">
    登录组件
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/pic.jpg" alt="头像不见了" />
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <!-- show-password有眼睛type="password"只有黑点 -->
          <el-input
            show-password
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="loginCheck">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<!-- 行为区域 -->
<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3-10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在5-20个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    loginCheck() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return
        const res = await this.$http.post('login', this.loginForm)
        console.log(res.data.meta.msg)
        console.log(res.data.meta.status)
        console.log(res.data)
        if (res.data.meta.status === 200) {
          // 提示登录信息
          this.$message({
            showClose: true,
            message: '登录成功',
            type: 'success'
          })
          // token保存到sessionStorage，localStorage是持久化
          window.sessionStorage.setItem('token', res.data.data.token)
          this.$router.push('/home')
        } else {
          this.$message({
            showClose: true,
            message: res.data.meta.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
<!--style -->
<style lang="less" scoped>
// less代表支持less语法，scroped控制样式生效区间，加上代表只在当前组件声效，去掉则全局生效
.login_container {
  background-color: #ffcccc;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px; // 圆角
  position: absolute; // 绝对定位，可以进行页面位移
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px;

  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    background-color: #fff;
    border-radius: 50%; // 此时成为圆形
    padding: 10px; //内边距，使得和图片之间有距离
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 10px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
